package sg.edu.nus.iss.jass.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import sg.edu.nus.iss.jass.privilege.SessionContext;
import sg.edu.nus.iss.jass.privilege.User;

/**
 * <h5>Main Application Class to launch the GUI</h5>
 * This is the class with main() method to launch the application GUI which
 * is inherited from JFrame. 
 * This Frame  will have MenuPanel on left to show the JTreeMenu for the use to 
 * select the Menu, RightPanel on right to launch the actual function screen
 * and StatusBar on the bottom to show any status messages.
 * 
 * This MainFrame will launch the LoginPanel to show the Login Window for the user to 
 * enter the credentials to authenticate before accessing the functions. 
 * @author Kafoor Sammil
 * 
 */
public class MainFrame extends JFrame /* implements TreeSelectionListener */{

	private static final long serialVersionUID = 8166256310887657566L;
	
	/**
     * The width of the main window.
     */ 
	public static final int WIDTH = 1000;
	
	/**
     * The height of the main window.
     */ 
	public static final int HEIGHT = 650;

	/**
     * The width of the main window margin.
     */ 
	
	public static final int WIDTH_MARGIN = 10;
	
	/**
     * The height of the main window margin.
     */ 
	public static final int HEIGHT_MARGIN = 10;

	/**
     * The MenuPanel to show the JTree Menu for selecting the function.
     */
	private MenuPanel menuPanel = null;
	
	/**
     * The JPanel to show the actual FunctionPanel on the right.
     */
	private JPanel rightPanel = null;
	
	/**
     * The JPanel to show the satusBar on the bottom.
     */
	private JLabel statusBarLabel = null;

	private FunctionPanel functionPanel = null;
	
	
	public void setUser(User myUser) {
		SessionContext.setUser(myUser);
		init();
	}
	/**
	 * To get the User Object of the current user logged in this application.
	 * 
	 * @return User Object from the current session context.
	 */
	public User getUser() {
		return SessionContext.getUser();
	}
	
	/**
	 * To auto resize the window according to the components loaded on the frame.
	 * 
	 */
	public void autoResize() {
		this.pack();
		this.setSize(this.getWidth() + WIDTH_MARGIN, this.getHeight()
				+ HEIGHT_MARGIN);
		this.setLocationRelativeTo(null);
	}
	
	/**
	 * To create the MainFrame object with the title passed as argument.
	 * @param title
	 *            title string of the Main Frame.
	 *  
	 */
	public MainFrame(String title) {
		super(title);
		setResizable(false);
		SessionContext.setUser(null);
		getContentPane().setLayout(new BorderLayout());
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent we) {
				dispose();
				System.exit(0);
			}
		});
		initData();
		//init();//
		initLogin();
		autoResize();
		setVisible(true);
	}
	
	/**
	 * This is only for junit test
	 */
	@Deprecated
	public MainFrame() {
		getContentPane().removeAll();
		getContentPane().invalidate();
		this.rightPanel = new JPanel();
		this.rightPanel.setLayout(new BorderLayout());
		this.functionPanel = new FunctionPanel(null);
		this.rightPanel.add(this.functionPanel, BorderLayout.WEST);
		this.rightPanel.setPreferredSize(new Dimension((int) (WIDTH * 0.80),
				HEIGHT));
		getContentPane().add(this.rightPanel, BorderLayout.EAST);
		getContentPane().add(statusBarPanel(), BorderLayout.SOUTH);
		autoResize();
		getContentPane().validate();
		
		setVisible(false);
	}

	private void initLogin() {
		getContentPane().removeAll();
		getContentPane().invalidate();
		LoginPanel loginPanel = new LoginPanel(this);
		getContentPane().add(loginPanel, BorderLayout.CENTER);
		//init();
	}

	private void init() {
		getContentPane().removeAll();
		getContentPane().invalidate();
		// if (SessionContext.getUser() != null) {

		// BUILD THE MENU TREE
		this.menuPanel = new MenuPanel(this);
		getContentPane().add(this.menuPanel, BorderLayout.CENTER);

		this.rightPanel = new JPanel();
		this.rightPanel.setLayout(new BorderLayout());
		this.functionPanel = createWelcomePanel();
		this.rightPanel.add(this.functionPanel, BorderLayout.WEST);
		this.rightPanel.setPreferredSize(new Dimension((int) (WIDTH * 0.80),
				HEIGHT));
		getContentPane().add(this.rightPanel, BorderLayout.EAST);
		getContentPane().add(statusBarPanel(), BorderLayout.SOUTH);

		// }
		autoResize();
		getContentPane().validate();
	}

	/**
	 * Returns the FunctionPanel of the Mainframe
	 * @return the functionPanel
	 */
	public FunctionPanel getFunctionPanel() {
		return functionPanel;
	}
	
	/**
	 * set the Status bar message on the Mainframe
	 * @param text the status bar message to set
	 */
	public void setStatusBar(String text) {
		this.statusBarLabel.setText(text);
	}
	
	/**
	 * To add the Form Panel to the Mainframe. This will be called by all the Use Case Form Panels
	 * to add the Form Panel into the Mainframe after creating the Form Panel.
	 * 
	 * @param formPanel the formPanel to add into the FunctionPanel
	 */
	public void addFormPanel(FormPanel formPanel) {
		// Remove Existing Fucntion Panel
		this.rightPanel.remove(this.functionPanel);
		this.functionPanel = new FunctionPanel(formPanel);
		this.rightPanel.add(this.functionPanel, BorderLayout.NORTH);
		this.validate();
	}
	
	protected HeaderPanel getHeaderPanel() {
		return this.functionPanel.getHeaderPanel();
	}

	private JPanel statusBarPanel() {
		JPanel statusBarPanel = new JPanel();
		statusBarPanel.setLayout(new BorderLayout());
		statusBarPanel.setPreferredSize(new Dimension(106, 20));
		this.statusBarLabel = new JLabel("READY");
		statusBarPanel.add(this.statusBarLabel, BorderLayout.NORTH);
		return statusBarPanel;
	}

	private FunctionPanel createWelcomePanel() {

		FunctionPanel welcomePanel = new FunctionPanel(null);
		welcomePanel.setLayout(null);
		welcomePanel.setPreferredSize(new Dimension(800, 600));
		JLabel custLabel = new JLabel();
		custLabel = new JLabel("You are logged in as " + SessionContext.getUser().getRole().getRoleName());

		
		custLabel.setBounds(300, 300, 400, 20);
		welcomePanel.add(custLabel);
		return welcomePanel;
	}
	
	/**
	 * To logout from the system
	 */
	public void doLogout() {
		dispose();
		System.exit(0);
	}

	private void initData() {

	}
	
	/**
	 * Main method to launch the application
     * @param argv  Command Line arguments passed to the main method
	 * 
	 */
	public static void main(String[] argv) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainFrame myExample = new MainFrame(
						"Job Advertisement Search System");
				myExample.setLocationRelativeTo(null);
				myExample.setVisible(true);
			}
		});
	}

}
